<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/static/frontend/lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/frontend/css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<form class="layui-form layuimini-form" autocomplete="off">
    <div class="layui-form-item">
        <label class="layui-form-label required">项目</label>
        <div class="layui-input-block">
            <select name="project" lay-verify="required" lay-search>
                <option value=""></option>
                {foreach $projects as $v}
                <option value="{$v.id}">{$v.name}</option>
                {/foreach}
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">时间</label>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="startTime" class="layui-input" id="startTime" placeholder="请选择开始时间" lay-verify="required">
            </div>
        </div>
        <div class="layui-inline">
            <div class="layui-input-inline">
                <input type="text" name="endTime" class="layui-input" id="endTime" placeholder="请选择结束时间" lay-verify="required">
            </div>
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">计算</button>
        </div>
    </div>
</form>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <table id="task-table" class="layui-table" lay-filter="task-table"></table>
        </div>
    </div>
</div>
<script src="/static/frontend/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script src="/static/frontend/js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script>
    layui.use(['form', 'layer', 'laydate', 'table', 'treetable'], function () {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.$,
            laydate = layui.laydate,
            table = layui.table,
            treetable = layui.treetable;

        //开始时间选择器
        laydate.render({
            elem: '#startTime'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        // 结束时间选择器
        laydate.render({
            elem: '#endTime'
            ,type: 'datetime'
            ,trigger: 'click'
        });

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            $.post('/frontend/task/compute-work-hour', data.field, function (res) {
                if (res.code == 0) {
                    // 计算成功
                    // 添加操作按钮
                    var htmlStr = `<div class="layui-btn-group operate">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
            </div>`;
                    $('.operate').remove();
                    $('table').before($(htmlStr));
                    // 给操作按钮绑定事件
                    $('#btn-expand').click(function () {
                        treetable.expandAll('#task-table');
                    });
                    $('#btn-fold').click(function () {
                        treetable.foldAll('#task-table');
                    });

                    // 渲染表格
                    layer.load(2);
                    treetable.render({
                        treeColIndex: 1,
                        treeSpid: 0,
                        treeIdName: 'id',
                        treePidName: 'pid',
                        elem: '#task-table',
                        height: 'full-108',
                        data: res.data,
                        toolbar: '#toolbarDemo',
                        page: false,
                        cols: [[
                            {field: 'id', minWidth: 80, title: 'ID'},
                            {field: 'name', minWidth: 100, title: '名称'},
                            {field: 'start_time', minWidth: 80, title: '开始时间'},
                            {field: 'end_time', minWidth: 80, title: '结束时间'},
                            {field: 'status', minWidth: 80, title: '状态'},
                            {field: 'time', minWidth: 80, title: '耗时'}
                        ]],
                        done: function () {
                            layer.closeAll('loading');
                        }
                    });

                    // 添加总工时计算内容
                    $('.totalTime').remove();
                    $('.layuimini-main>div').after($(`<div class="totalTime">总工时：${res.totalTime}</div>`));
                } else if (res.code == 302) {
                    location.href = '/frontend/error/' + res.msg;
                } else {
                    layer.msg(res.msg, {icon: 2});
                }
            }, 'json');
            return false;
        });
    });
</script>
</body>
</html>